package com.spt.ws.task;

import java.util.Map;

import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

import com.spt.ws.tasks.ITask;
@DisallowConcurrentExecution
public class QuartzTaskFactory implements Job {

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        try {
            System.out.println("任务运行...");
            Map<String, Object> scheduleJob = (Map<String, Object>) context.getMergedJobDataMap().get("scheduleJob");
            ITask bean = null;
            WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
            if(wac  == null) return;
            bean = (ITask) wac.getBean((String) scheduleJob.get("BEAN"));
            
            //System.out.println("任务名称: [" + scheduleJob.get("JOBNAME") + "]  " );
            bean.doTask();
            //在这里执行你的任务...
            } catch (Exception e) {
            e.printStackTrace();
           }

    }

}
